from django.db import models
from datetime import datetime
from goods.models import Goods
from django.contrib.auth import get_user_model
User = get_user_model()
# Create your models here.


class UserFav(models.Model):
    """
    用户收藏
    """
    user = models.ForeignKey(User, db_constraint=False, help_text='用户')
    goods = models.ForeignKey(Goods, db_constraint=False, help_text='商品')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间')

    class Meta:
        verbose_name = '用户收藏'
        verbose_name_plural = verbose_name
        unique_together = ('user', 'goods')

    def __str__(self):
        name = self.user.name if self.user.name else self.user.username
        return name


class UserLeavingMessage(models.Model):
    """
    用户留言
    """
    MessageType = (
        ("1", "留言"),
        ("2", "投诉"),
        ("3", "询问"),
        ("4", "售后"),
        ("5", "求购")
    )
    user = models.ForeignKey(User, db_constraint=False, help_text='用户')
    message_type = models.CharField(max_length=10, choices=MessageType, default='1',
                                    help_text='留言类型("1", 留言),''("2", "投诉"),("3", "询问"),("4", "售后"),("5", "求购")')
    subject = models.CharField(max_length=100, null=True, blank=True, help_text='主题')
    message = models.TextField(null=True, blank=True, help_text='留言内容')
    file = models.FileField(upload_to="message/images/", help_text='上传文件')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间')

    class Meta:
        verbose_name = "用户留言"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s(%s)".format(self.user.name, self.subject)


class UserAddress(models.Model):
    """
    用户收货地址
    """
    user = models.ForeignKey(User, db_constraint=False, help_text='用户')
    province = models.CharField(max_length=100, null=True, blank=True, help_text='省份')
    city = models.CharField(max_length=100, null=True, blank=True, help_text='城市')
    address = models.CharField(max_length=100, null=True, blank=True, help_text='详细地址')
    signer_name = models.CharField(max_length=100, null=True, blank=True, help_text='收货人')
    signer_mobile = models.CharField(max_length=11, null=True, blank=True, help_text='联系电话')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间')

    class Meta:
        verbose_name = "用户收货地址"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.address
